//
//  OIPubListModel.m
//  EducationOID
//
//  Created by slz on 2017/5/25.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIPubListModel.h"

@implementation OIPubListModel

- (Class)classOfItem{
    return [OIPubListItem class];
}

-(TGMBaseModelActionData*)actionDataWithPub:(BOOL)isPub{
    return [TGMBaseModelActionData dataWithApi:isPub ? kAPI_PUBLIC_OIDINFO :kAPI_PUT_OIDINFO httpMethod:kRequestHttpMethodGet params:nil dataId:^NSString *(NSString *dataId) {
        return @"OIPubListModel_list_key";
    }];
}

-(NSString *)keyOfListData{
    return @"rows";
}

- (BOOL)processSuccRawData:(NSData *)rawData parsedJson:(MFWJson *)jsonObj forTask:(MFWHttpTask *)task{
    BOOL ret = NO;
    ret = [self processServiceSuccRawData:rawData parsedJson:jsonObj forTask:task];
    if (ret)
    {
        //设置dataStatus
        NSString *dataId = task.dataIdentifier.dataId;
        MFWDataStatus *dataStatus = [self dataStatusForDataId:dataId];
        
        if (task.dataRequestType == kMFWDataRequest_Refresh)
        {// refresh
            dataStatus.refreshDate = [NSDate date];
            dataStatus.statusType = kDataStatus_SuccRefresh;
        }
        else
        {// get more
            dataStatus.statusType = kDataStatus_SuccGetmore;
            
            MFWJson *jsonData = jsonObj;
            NSString *keyOfList = [self keyOfListData];
            if (![MFWStringUtils isEmptyString:keyOfList])
            {
                if ([[jsonData jsonForKey:keyOfList] count] == 0)
                {
                    dataStatus.statusType = kDataStatus_DataEnd;
                }
            }
        }
    }
    return  ret;
}

-(BOOL)processServiceSuccRawData:(NSData *)rawData parsedJson:(MFWJson *)jsonObj forTask:(MFWHttpTask *)task{
    NSString *listKey = [self keyOfListData];
    NSString *dataId = task.dataIdentifier.dataId;

    if (![MFWStringUtils isEmptyString:listKey])
    {//非列表型数据
        [_dataDic setValue:jsonObj.jsonObj forKey:dataId];
    }
    
    if ([jsonObj hasValueForKey:listKey])
    {//列表型数据
        
        NSArray *parsedModelItemArray = [self parseModelItemArrayFromJsonArray:[jsonObj jsonForKey:listKey]];
        
        task.itemArrayData = parsedModelItemArray;
        
        if (task.dataRequestType == kMFWDataRequest_Refresh)
        {// refresh
            [_parsedModelItemDataDic setValue:parsedModelItemArray forKey:dataId];
        }

    }
    return YES;
}
    
- (NSArray*)parseModelItemArrayFromJsonArray:(MFWJson*)jsonArray
{
    return [TGMBaseModel parseModelItemArrayFromJsonArray:jsonArray itamClass:[self classOfModelItemForDataId:0]];
}

+ (NSArray*)parseModelItemArrayFromJsonArray:(MFWJson*)jsonArray itamClass:(Class)clazz;
{
    if ([jsonArray.jsonObj isKindOfClass:[NSArray class]])
    {
        if (clazz == nil)
        {
            return [jsonArray.jsonObj copy];
        }
        else
        {
            NSUInteger count = [jsonArray count];
            NSMutableArray *modelItemArray = [NSMutableArray arrayWithCapacity:count];
            
            for (NSUInteger i = 0; i < count; i++)
            {
                MFWJson *jsonItem = [jsonArray jsonAtIndex:i];
                id modelItem = [[clazz alloc] initWithJson:jsonItem];
                [modelItemArray addObject:modelItem];
            }
            return modelItemArray;
        }
    }
    return nil;
}


@end
